<template>
  <el-autocomplete
    style="width:100%"
    popper-class="my-autocomplete"
    v-model="state3"
    :fetch-suggestions="querySearch"
    placeholder="请选择图标"
    @select="handleSelect">
    <i
      class="el-icon-edit el-input__icon"
      slot="suffix"
      @click="handleIconClick">
    </i>
    <template slot-scope="{ item }">
      <div class="name">
          <svg-icon  :icon-class="item.value" class-name="disabled" />
          &nbsp;&nbsp;&nbsp;&nbsp;
          {{ item.value }}
      </div>
    </template>
  </el-autocomplete>
</template>
<style>
.my-autocomplete {
  li {
    line-height: normal;
    padding: 7px;

    .name {
      text-overflow: ellipsis;
      overflow: hidden;
    }
    .addr {
      font-size: 12px;
      color: #b4b4b4;
    }

    .highlighted .addr {
      color: #ddd;
    }
  }
}
</style>

<script>
  import icons from './icons'
  export default {
    props:{
      defaultIcon:String
    },
    data() {
      return {
        restaurants: [],
        state3: this.defaultIcon == undefined ? '' : this.defaultIcon
      };
    },
    methods: {
      querySearch(queryString, cb) {
        var restaurants = this.restaurants;
        var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
        // 调用 callback 返回建议列表的数据
        cb(results);
      },
      createFilter(queryString) {
        return (restaurant) => {
          return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
        };
      },
      loadAll() {
        let arr = [];
        for (var i = icons.length - 1; i >= 0; i--) {
            arr.push({
              value:icons[i],
            })
        }
        return arr;
      },
      handleSelect(item) {
        this.$emit('IconChoice',item)
      },
      handleIconClick(ev) {

      }
    },
    mounted() {
      this.restaurants = this.loadAll();
    }
  }
</script>
